package com.atstudying.mall.product.controller;

import com.atstudying.common.utils.PageUtils;
import com.atstudying.common.utils.R;
import com.atstudying.mall.product.entity.ProductAttrValueEntity;
import com.atstudying.mall.product.service.AttrService;
import com.atstudying.mall.product.service.ProductAttrValueService;
import com.atstudying.mall.product.vo.AttrRespVo;
import com.atstudying.mall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 商品属性
 *
 * @author zlj
 * @email 2908434305@qq.com
 * @date 2020-10-04 22:40:50
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    ProductAttrValueService productAttrValueService;

    @GetMapping("/base/listforspu/{spuId}")
    public R baseAttrlistforspu(@PathVariable("spuId") Long spuId){

        List<ProductAttrValueEntity> entities= productAttrValueService.baseAttrlistforspu(spuId);
        return R.ok().put("data",entities);
    }
    /**
     * 实现模糊查询和字段查询//attr_id attr_name catelogId
     * @param params
     * @param catelogId
     * @return
     */
    @GetMapping("/{attrType}/list/{catelogId}")
    public R baseAttrList(@RequestParam Map<String,Object> params,@PathVariable("catelogId") Long catelogId,@PathVariable("attrType")String type){

        PageUtils page = attrService.queryBaseAttrPage(params,catelogId, type);

        return R.ok().put("page", page);    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
   // @RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
        //AttrEntity返回的数据太少了，我们用自己的vo
//		AttrEntity attr = attrService.getById(attrId);

        AttrRespVo attr=attrService.getAttrInfo(attrId);
        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
   // @RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attr){
        //M
		attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
   // @RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVo attr){
		attrService.updateAttr(attr);

        return R.ok();
    }

    @PostMapping("/update/{spuId}")
    // @RequiresPermissions("product:attr:update")
    public R updateSpuAttr(@RequestBody List<ProductAttrValueEntity> attr,@PathVariable("spuId") Long spuId){

        productAttrValueService.updateSpuAttr(spuId,attr);

        return R.ok();
    }
    /**
     * 删除
     */
    @RequestMapping("/delete")
   // @RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
